package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zg3zhoukao/dao"
	"zg3zhoukao/request"
)

func ArticleAdd(c *gin.Context) {
	var data request.ArticleAdd
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  err.Error(),
			"Data": nil,
		})
		return
	}
	add, err := dao.ArticleAdd(data.UserId, data.Title, data.Text)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  "文章发布失败",
			"Data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "文章发布成功",
		"Data": add,
	})
	return
}

func ArticleHistory(c *gin.Context) {
	var data request.ArticleHistory
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  err.Error(),
			"Data": nil,
		})
		return
	}
	add, err := dao.ArticleHistory(data.UserId, data.ArticleId)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  "文章浏览失败",
			"Data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "文章浏览成功",
		"Data": add,
	})
	return
}

func ArticleGood(c *gin.Context) {
	var data request.ArticleGood
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  err.Error(),
			"Data": nil,
		})
		return
	}
	add, err := dao.ArticleGood(data.UserId, data.ArticleId)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  "文章点赞失败",
			"Data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "文章点赞成功",
		"Data": add,
	})
	return
}

func ReviewAdd(c *gin.Context) {
	var data request.ReviewAdd
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  err.Error(),
			"Data": nil,
		})
		return
	}
	add, err := dao.ReviewAdd(data.UserId, data.ArticleId, data.ConText)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  "文章评论失败",
			"Data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "文章评论成功",
		"Data": add,
	})
	return
}

func ReviewAddAdd(c *gin.Context) {
	var data request.ReviewAddAdd
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  err.Error(),
			"Data": nil,
		})
		return
	}
	add, err := dao.ReviewAddAdd(data.UserId, data.ArticleId, data.FatherId, data.ConText)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  "文章评论回复失败",
			"Data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "文章评论回复成功",
		"Data": add,
	})
	return
}

func ReviewGood(c *gin.Context) {
	var data request.ReviewGood
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  err.Error(),
			"Data": nil,
		})
		return
	}
	add, err := dao.ReviewGood(data.UserId, data.ReviewId)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  "评论点赞失败",
			"Data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "评论点赞成功",
		"Data": add,
	})
	return
}

func ArticleList(c *gin.Context) {
	var data request.ArticleList
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  err.Error(),
			"Data": nil,
		})
		return
	}
	add, err := dao.ArticleList(data.Title)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  "列表展示失败",
			"Data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "列表展示成功",
		"Data": add,
	})
	return
}

func ReviewDel(c *gin.Context) {
	var data request.ReviewDel
	if err := c.ShouldBind(&data); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  err.Error(),
			"Data": nil,
		})
		return
	}
	_, err := dao.ReviewDel(data.Id)

	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 0,
			"Msg":  "评论删除失败",
			"Data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "评论删除成功",
		"Data": nil,
	})
	return
}
